package com.sky.service.impl;

import com.sky.dto.GoodsSalesDTO;
import com.sky.entity.Orders;
import com.sky.mapper.ReportMapper;
import com.sky.service.ReportService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.SalesTop10ReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private ReportMapper reportMapper;


    @Override
    public TurnoverReportVO getTurnover(LocalDate begin, LocalDate end) {
        //准备时间容器 查询一段时间
        List<LocalDate> dateList = new ArrayList<>();
        dateList.add(begin);

        while (!begin.equals(end)){
            begin = begin.plusDays(1);
            dateList.add(begin);
        }
        //营业额获取
        List<Double> turnoverList = new ArrayList<>();
        //从时间容器中获取每一天的时间
        for (LocalDate date : dateList) {
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
            Map map = new HashMap();
            map.put("begin",beginTime);
            map.put("end",endTime);
            //或者正营业状态所获金额
            map.put("status", Orders.COMPLETED);
            //使用聚合函数获取每一天所获营业额的总值
            Double turnover = reportMapper.getTurnover(map);
            //保证数据的安全性和可行性
            turnover = turnover == null ? 0.0 : turnover;
            //获取一天的总额加入容器
            turnoverList.add(turnover);
        }
        //返回值为对象TurnoverReportVo 把获得的数据进行封装返回
        return TurnoverReportVO.
                builder().
                dateList(StringUtils.join(dateList,",")).
                turnoverList(StringUtils.join(turnoverList,",")).
                build();
    }
    //用户统计 具体到每一天的增长量
    @Override
    public UserReportVO getUserStatistics(LocalDate begin, LocalDate end) {
        //准备时间容器 查询一段时间
        List<LocalDate> dateList = new ArrayList<>();
        dateList.add(begin);

        while (!begin.equals(end)){
            begin = begin.plusDays(1);
            dateList.add(begin);
        }
        //新用户
        List<Integer> newUserList = new ArrayList<>();
        //用户总数
        List<Integer> totalUserList = new ArrayList<>();
        for (LocalDate date : dateList) {
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
            //特定区间增长用户量
            Integer newUser = getUserIdCount(beginTime, endTime);
            //特定区间最大时间单位统计用户量
            Integer totalUser = getUserIdCount(null, endTime);
            //循环内数据一一对应
            newUserList.add(newUser);
            totalUserList.add(totalUser);
        }
        //封装成返回值返回数据
        return UserReportVO.
                builder().
                dateList(StringUtils.join(dateList,",")).
                totalUserList(StringUtils.join(totalUserList,",")).
                newUserList(StringUtils.join(newUserList,",")).
                build();
    }

    @Override
    public OrderReportVO getOrdersStatistics(LocalDate begin, LocalDate end) {
        //准备时间容器 查询一段时间
        List<LocalDate> dateList = new ArrayList<>();
        dateList.add(begin);

        while (!begin.equals(end)){
            begin = begin.plusDays(1);
            dateList.add(begin);
        }
        //总订单
        List<Integer> orderCountList = new ArrayList<>();
        //有效订单
        List<Integer> validOrderCountList = new ArrayList<>();
        for (LocalDate date : dateList) {
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
            //select count(id) from orders where order_time< ? and order_time>?
            Integer orderCount = getOrdersIdCount(beginTime, endTime,null);
            //select count(id) from orders where order_time< ? and order_time>? and status =5
            Integer validOrderCount = getOrdersIdCount(beginTime, endTime, Orders.COMPLETED);
            orderCountList.add(orderCount);
            validOrderCountList.add(validOrderCount);
        }
/*        Integer totalOrderCount=0;
        Integer validOrderCount = 0;
        for (int i = 0; i < orderCountList.size(); i++) {
            totalOrderCount +=i;
        }
        for (int i = 0; i < validOrderCountList.size(); i++) {
            validOrderCount += i;
        }*/
        Integer totalOrderCount = orderCountList.stream().reduce(Integer::sum).get();
        Integer validOrderCount = validOrderCountList.stream().reduce(Integer::sum).get();
        Double orderCompletionRate= 0.0;
        if (totalOrderCount!=0){
            orderCompletionRate = validOrderCount.doubleValue() / totalOrderCount;
        }
        return OrderReportVO.
                builder().
                dateList(StringUtils.join(dateList,",")).
                orderCountList(StringUtils.join(orderCountList,",")).
                validOrderCountList(StringUtils.join(validOrderCountList,",")).
                totalOrderCount(totalOrderCount).
                validOrderCount(validOrderCount).
                orderCompletionRate(orderCompletionRate).
                build();
    }
    //top10 热卖品
    @Override
    public SalesTop10ReportVO getSalesTop10(LocalDate begin, LocalDate end) {
        //创建时间容器
        LocalDateTime beginTime = LocalDateTime.of(begin, LocalTime.MIN);
        LocalDateTime endTime = LocalDateTime.of(end, LocalTime.MIN);
        //获得时间容器内符合条件的所有数据
        List<GoodsSalesDTO> goodsSalesDTOList = reportMapper.getSalesTop10(beginTime,endTime);
        //拆分数据
        String nameList = StringUtils.join(goodsSalesDTOList.stream().map(GoodsSalesDTO::getName).collect(Collectors.toList()),",");
        String numberList = StringUtils.join(goodsSalesDTOList.stream().map(GoodsSalesDTO::getNumber).collect(Collectors.toList()),",");
        //封装并返回
        return SalesTop10ReportVO.
                builder().
                nameList(nameList).
                numberList(numberList).
                build();
    }

    /**
     * 封装一个私有的返回值为Integer的方法
     * @param beginTime
     * @param endTime
     * @return
     */
    private Integer getUserIdCount(LocalDateTime beginTime,LocalDateTime endTime){
        Map map = new HashMap();
        map.put("begin",beginTime);
        map.put("end",endTime);
        return reportMapper.getUserIdCount(map);
    }

    /**
     * 订单计数方法
     */
    private Integer getOrdersIdCount(LocalDateTime beginTime,LocalDateTime endTime,Integer status){
        Map map = new HashMap();
        map.put("status",status);
        map.put("begin",beginTime);
        map.put("end",endTime);
        return reportMapper.getOrdersIdCount(map);
    }

}
